<?php

namespace App\Kuafu\Mybank\Controller\Merchant;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Export\Model\ExportDownloadModel;
use App\Kuafu\Export\Service\ExportDownloadService;
use App\Kuafu\Mybank\Service\MybankService;
use App\Kuafu\Mybank\Validate\MybankValidate;
use App\Kuafu\Order\Enum\OrderMerchantEnum;
use App\Kuafu\Shop\Enum\ShopCodeEnum;
use App\Kuafu\User\Enum\UserCodeEnum;
use App\Kuafu\User\Exceptions\UserException;
use App\Kuafu\User\Service\AuthService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class MybankController extends BaseController
{
    /**
     * 钱包明细
     * @param Request $request
     * @return JsonResponse
     * @throws UserException
     */
    public function wallet(Request $request): JsonResponse
    {
        $userInfo   = AuthService::getAuthUser();
        $shopId     = $request->header('shopId', 0);
        $userId     = $userInfo['id'] ?? "";
        if (!$userId) {
            return $this->fail(UserCodeEnum::ERR_USER_NOT_EXIST, UserCodeEnum::CODE_TEXT[UserCodeEnum::ERR_USER_NOT_EXIST]);
        }
        return $this->success(MybankService::wallet($userId, $shopId));
    }

    /**
     * 添加查看日志
     * @param Request $request
     * @return JsonResponse
     * @throws UserException
     */
    public function addClickLog(Request $request): JsonResponse
    {
        $userInfo   = AuthService::getAuthUser();
        $userId     = $userInfo['id'] ?? "";

        $shopId     = $request->header('shopId', 0);

        MybankService::addClickLog($userId, $shopId);
        return $this->success();
    }

    /**
     * 账单明细列表
     * @param Request $request
     * @return JsonResponse
     */
    public function billList(Request $request): JsonResponse
    {
        $startDate  = (string)$request->get('startDate', '');
        $endDate    = (string)$request->get('endDate', '');
        $shopId     = (string)$request->get('shopId', '');
        $type       = (string)$request->get('type', '');
        $pageSize   = (int)$request->get('pageSize', 20);
        $pageNum    = (int)$request->get('pageNum', 1);
        $merchantId = (string)$request->get('entryId', '');
        $export     = (int)$request->get('export', 0);
        $valid      = MybankValidate::billList($request);
        if ($valid) {
            return $this->fail(ShopCodeEnum::ERR_SHOP_INPUT, $valid);
        }
        $shopIds = [];
        if ($shopId && $shopId != '-1') {//店铺id 默认-1
            $shopIds = explode(',', $shopId);
        }
        if ($type && $type != '-1') {
            // 记录类型 walletRecordType
            $type = explode(',', $type);
        } else {
            $type = [];
        }
        if ($export) {
            $params = [
                'merchantId' => $merchantId,
                'startDate'  => $startDate,
                'endDate'    => $endDate,
                'shopIds'    => $shopIds,
                'type'       => $type,
                'pageSize'   => $pageSize,
                'pageNum'    => $pageNum,
                'export'     => $export,
            ];

            $completeTimeStart = strtotime($startDate);
            $completeTimeEnd   = strtotime($endDate);
            ExportDownloadService::export(ExportDownloadModel::CLIENT_TYPE_APP,
                ExportDownloadModel::FILE_TYPE_APP_MYBANK_WALLET_RECORD, 0,
                OrderMerchantEnum::SHOW_TIME_TAB_DEFINE, $completeTimeStart, $completeTimeEnd, $shopIds, $params);
            return $this->success();
        }
        return $this->success(MybankService::billList($merchantId, $startDate, $endDate, $shopIds, $type, $pageSize,
            $pageNum, $export));
    }

    /**
     * 账单明细详情
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function billDetail(Request $request, int $id): JsonResponse
    {
        return $this->success(MybankService::billDetail($id));
    }
}
